<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/28
 * Time: 下午 02:08
 */

namespace app\admin\model;


use think\Model;

class Activity extends Model
{
    /**
     * @return mixed
     * 活动数据
     */
    public function activity_list($data)
    {
        $list = db("activity")
            ->where("is_delete",0)
            ->where(function($query) use ($data){
                if(!empty($data)){
                    $query->where("activity_name","like","%".$data."%");
                }
            })
            ->order('update_time desc')
            ->select();
        return $list;
    }

    /**
     * @return mixed
     * 活动数量
     */
    public function activity_count($data)
    {
        $count = db("activity")
                ->where("is_delete",0)
                ->where(function($query) use ($data){
                        if(!empty($data)){
                            $query->where("activity_name","like","%".$data."%");
                        }
                })
                ->count();
        return $count;
    }

    /**
     * @param $info
     * @return int|string
     * @throws \Exception
     * 添加
     */
    public function doAdd($info)
    {
        $info['create_time'] = $info['update_time'] = time();
        $info['opentime'] = strtotime($info['opentime']);
        $info['closetime'] = strtotime($info['closetime']);
        if($info['opentime'] < time()){
            exception('开始时间不能小于当前时间');
        }
        if($info['closetime'] < time()){
            exception('结束时间不能小于当前时间');
        }
        $one = db("activity")
            ->where('opentime','<',$info['opentime'])
            ->where('is_use',1)
            ->where('is_delete',0)
            ->find();
        if($one){
            exception('当前有活动正在进行中');
        }
        $info['is_use'] = 1;
        $new_add = db("activity")
                    ->insert($info);
        if(!$new_add){
            exception("添加失败");
        }
        return $new_add;
    }

    /**
     * @param $info
     * @return int|string
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     * 修改
     */
    public function doEdit($info)
    {
        $info['update_time'] = time();
        $id = $info['activity_id'];
        unset($info['activity_id']);
        $info['opentime'] = strtotime($info['opentime']);
        $info['closetime'] = strtotime($info['closetime']);
        if($info['opentime'] < time() && $info['closetime'] > time()){
            exception('活动已开始，无法修改');
        }
        if($info['closetime'] < time()){
            exception('活动已结束，无法修改');
        }
        if($info['opentime'] < time()){
            exception('开始时间不能小于当前时间');
        }
        if($info['closetime'] < time()){
            exception('结束时间不能小于当前时间');
        }
        $one = db("activity")
            ->where('opentime','<',$info['opentime'])
            ->where('is_use',1)
            ->where('is_delete',0)
            ->find();
        if($one){
            exception('当前有活动正在进行中');
        }
        $info['is_use'] = 1;
        $new_edit = db("activity")
            ->where("id",$id)
            ->update($info);
        if(!$new_edit){
            exception("更新失败");
        }
        return $new_edit;
    }

    /**
     * @param $id
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     * 删除
     */
    public function dodelete($id)
    {
        $result = db('activity')
            ->where('id',$id)
            ->delete();
        if(!$result){
            exception("删除失败");
        }
    }
    /**
     * 状态-启用&禁用
     */
    public function editStatus($data)
    {
        $data['update_time'] = time();
        $result = db("activity")->update($data);
        if(!$result){
            exception("操作失败");
        }
        return $result;
    }
}